<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/8
 * Time: 14:46
 */
namespace app\api\controller;
use think\Db;
use think\Request;
use app\api\model\Method;
use app\api\model\Message;
class My extends Nocommon
{
    /*
     * 我的首页 接口
     */
    public function my()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            //查询用户
            $res = Db::name('user')->field('nickname,user_icon,sex,food_store_id,tour_id,team_id,hotel_id,is_card')->where('uid',$post['uid'])->find();
            if(!empty($res['food_store_id'])){
                $res['status'] = 3;
            }

            if(!empty($res['food_store_id']) || !empty($res['tour_id']) || !empty($res['team_id']) || !empty($res['hotel_id'])){
                $res['type'] = 1;
            }else{
                if(!empty($res['is_card'])){
                    $res['type'] = 3;
                }else{
                    $user = Db::name('my_card')->field('is_pass')->where('uid',$post['uid'])->find();
                    if(empty($user)){
                        $res['type'] = 4;
                    }else{
                        if($user['is_pass']==0){
                            $res['type'] = 2;
                        }else if($user['is_pass']==2){
                            $res['type'] = 4;
                        }
                    }

                }

            }
            //查询用户是否有未读信息
            if(empty($post['uid'])){
                $res['order_msg'] = 0;
                $res['system_msg'] =0;
                $res['h_msg'] = 0;
            }else {

                //查询是否有已读维度的消息
                $order_msg = Db::name('message')
                    ->where('receive_user_id', $post['uid'])
                    ->where('is_read', 0)
                    ->where('msg_type', 2)
                    ->select();
                if ($order_msg) {
                    $res['order_msg'] = 1;
                } else {
                    $res['order_msg'] = 0;
                }
                //系统消息
                $system_msg = Db::name('message')
                    ->where('receive_user_id', $post['uid'])
                    ->where('is_read', 0)
                    ->where('msg_type', 1)
                    ->select();
                if ($system_msg) {
                    $res['system_msg'] = 1;
                } else {
                    $res['system_msg'] = 0;
                }

                //互动消息
                $h_msg1 = Db::name("comment_message")->where("uid", $post['uid'])->where("is_read", 0)->select();

                $h_msg2 = Db::name("comment_method_message")->where('uid', $post['uid'])->where('is_read', 0)->select();

                $h_msg3 = Db::name("log_like")->where("uid", $post['uid'])->where("is_read", 0)->select();
                if ($h_msg1 || $h_msg2 || $h_msg3) {
                    $res['h_msg'] = 1;
                } else {
                    $res['h_msg'] = 0;
                }
            }

            unset($res['food_store_id']);
            unset($res['tour_id']);
            unset($res['team_id']);
            unset($res['hotel_id']);
            unset($res['is_card']);
            $this->apiReturn('200','成功',$res);
        }
    }

    /*
     * 修改头像
     */
    public function update_icon()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $data = [
                'user_icon'=>$post['user_icon']
            ];
            $res = Db::name('user')->where('uid',$post['uid'])->update($data);
            if($res){
                $this->apiReturn('200','修改成功');
            }else{
                $this->apiReturn('400','修改失败');
            }
        }
    }

    /*
     * 修改昵称
     */
    public function update_nickname()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $data = [
                'nickname'=>$post['nickname']
            ];
            $res = Db::name('user')->where('uid',$post['uid'])->update($data);
            if($res){
                $this->apiReturn('200','修改成功');
            }else{
                $this->apiReturn('400','修改失败');
            }
        }
    }

    /*
     * 修改性别
     */
    public function update_sex()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $data = [
                'sex'=>$post['sex']
            ];
            $res = Db::name('user')->where('uid',$post['uid'])->update($data);
            if($res){
                $this->apiReturn('200','修改成功');
            }else{
                $this->apiReturn('400','修改失败');
            }
        }
    }

    /*
     * 我的攻略
     */
    public function my_method()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $page = $this->p();
            $my = new Method();
            $method = $my->myMethod($post['uid'],$page);
            $this->apiReturn('200','成功',$method);
        }
    }

    /*
     * 我的消息
     */
    public function message()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $type = input('type','1','trim');
            $page = $this->p();
            $message = new Message();
            if($type==1){
                $data = $message->notice($post['uid'],$type,$page);
            }elseif($type==2){
                $data = $message->notice($post['uid'],$type,$page);
            }
            $this->apiReturn('200','成功',$data);
        }
    }
    /*
     * 我的发布
     */
    public function publish()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $type = input('type','1','trim');
            $page = $this->p();
            //攻略的
            if($type==1){
                $res = Db::name('method')->field('mid,title,image,update_time,is_pass,set_like_num,
                like_num,set_share_num,share_num,mtext')
                    ->where('is_del',0)
                    ->where('is_display',0)
                    ->where('uid',$post['uid'])
                    ->limit(($page-1)*config('page_size'),config('page_size'))
                    ->order('create_time','desc')
                    ->select();
                if(!empty($res)){
                    //获取评论数
                    foreach($res as $k=>$v){
                        $res[$k]['comment'] = Db::name('comment_method')->where('wid',$v['mid'])
                            ->where('pid',0)->where('is_del',0)->where('is_display',0)
                            ->count();
                        $res[$k]['like_num'] = (int)$v['set_like_num']+$v['like_num'];
                        $res[$k]['share_num'] = (int)$v['set_share_num']+$v['share_num'];
                        $res[$k]['update_time'] = time_ago($v['update_time']);
                        unset($res[$k]['set_like_num']);
                        unset($res[$k]['set_share_num']);
                        $res[$k]['video'] = '';
                        $res[$k]['video_covers'] = '';
                        $res[$k]['image'] = explode(',',$v['image']); 
                        if (empty($res[$k]['image'][0])) {
                            $res[$k]['image'] = [];
                        }
                           if(count($res[$k]['image'])>0){
                            foreach ($res[$k]['image'] as $keys => $value) {
                                 $res[$k]['image'][$keys] = addWaterByField($value);
                            } 
                         
                        }
                    
                    }
                }

            }
            if($type==2){
                $res = Db::name('friends_circle')->field('f_id,fc_title,image,video,set_like_num,like_num,
                set_share_num,share_num,is_pass,update_time,is_pass,content as mtext,video_covers')
                    ->where('is_del',0)
                    ->where('is_display',0)
                    ->where('uid',$post['uid'])
                    ->where('status',1)
                    ->limit(($page-1)*config('page_size'),config('page_size'))
                    ->order('create_time','desc')
                    ->select();
                if(!empty($res)){
                    //获取评论数
                    foreach($res as $k=>$v){
                        $res[$k]['comment'] = Db::name('comment_circle')->where('wid',$v['f_id'])
                            ->where('pid',0)->where('is_del',0)->where('is_display',0)
                            ->count();
                        $res[$k]['mid'] = $v['f_id'];
                        $res[$k]['title'] = $v['fc_title'];
                        $res[$k]['like_num'] = (int)$v['set_like_num']+$v['like_num'];
                        $res[$k]['share_num'] = (int)$v['set_share_num']+$v['share_num'];
                        $res[$k]['update_time'] = time_ago($v['update_time']);
                        $res[$k]['image'] = explode(',',$v['image']); 
                         if (empty($res[$k]['image'][0])) {
                            $res[$k]['image'] = [];
                        }     
                        if(count($res[$k]['image'])>0){
                            foreach ($res[$k]['image'] as $keys => $value) {
                                 $res[$k]['image'][$keys] = addWaterByField($value);
                            } 
                         
                        } 
                        unset($res[$k]['set_like_num']);
                        unset($res[$k]['set_share_num']);
                        unset($res[$k]['f_id']);
                        unset($res[$k]['fc_title']);
                    }

                }
            }
            if($type==3){
                $res = Db::name('friends_circle')->field('f_id,fc_title,image,video,set_like_num,like_num,
                set_share_num,share_num,is_pass,update_time,is_pass,video_covers,video_content as mtext')
                    ->where('is_del',0)
                    ->where('is_display',0)
                    ->where('uid',$post['uid'])
                    ->where('status',2)
                    ->limit(($page-1)*config('page_size'),config('page_size'))
                    ->order('create_time','desc')
                    ->select();
                if(!empty($res)){
                    //获取评论数
                    foreach($res as $k=>$v){
                        $res[$k]['comment'] = Db::name('comment_circle')->where('wid',$v['f_id'])
                            ->where('pid',0)->where('is_del',0)->where('is_display',0)
                            ->count();
                        $res[$k]['mid'] = $v['f_id'];
                        $res[$k]['title'] = $v['fc_title'];
                        $res[$k]['like_num'] = (int)$v['set_like_num']+$v['like_num'];
                        $res[$k]['share_num'] = (int)$v['set_share_num']+$v['share_num'];
                        $res[$k]['update_time'] = time_ago($v['update_time']);
                        $res[$k]['image'] = explode(',',$v['image']);
                         if (empty($res[$k]['image'][0])) {
                            $res[$k]['image'] = [];
                        }
                        if(count($res[$k]['image'])>0){
                            foreach ($res[$k]['image'] as $keys => $value) {
                                 $res[$k]['image'][$keys] = addWaterByField($value);
                            }  
                        }else{
                            $res[$k]['image'] = [];
                        }
                        unset($res[$k]['set_like_num']);
                        unset($res[$k]['set_share_num']);
                        unset($res[$k]['f_id']);
                        unset($res[$k]['fc_title']);
                    }

                }
            }
            $this->apiReturn('200','成功',$res);
        }
    }
    /*
     * 互动消息 评论我的
     */
    public function engage_message()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $page = $this->p();
            Db::name("comment_message")->where("uid",$post['uid'])->setField("is_read",1);
            Db::name("comment_method_message")->where('uid',$post['uid'])->setField('is_read',1);
            $where['t1.uid']  = $post['uid'];
            $data1 = Db::name('comment_method_message')->alias('t1')
                ->field("t1.type,t1.comment_content,t1.self_comment,t1.time,t4.nickname,t2.title,t2.image,t2.mid as mid,t4.user_icon")
                ->join('method t2','t1.wid = t2.mid','left')
                ->join('user t4','t1.comment_uid = t4.uid','left')
                //->limit(($page-1)*config('page_size'),config('page_size'))
                ->where($where)
                ->order('time','desc')
                ->select();
            if(!empty($data1)){
                foreach($data1 as $k=>$v){
                   // $data1[$k]['time'] = parent::time_ago($v['time']);
                    $data1[$k]['video_covers'] = '';
                    $data1[$k]['status'] = 0;
                    $data1[$k]['is_yq'] = 1;
                }
            }

            $data2 = Db::name('comment_message')->alias('t1')
                ->field('t1.type,t1.comment_content,t1.self_comment,t4.nickname,
                t2.fc_title as title,t2.image,t2.f_id as mid,t4.user_icon,t2.video_covers,t2.status,t1.time')
                ->join('friends_circle t2','t1.wid = t2.f_id','left')
                ->join('user t4','t1.comment_uid = t4.uid','left')
                //->limit(($page-1)*config('page_size'),config('page_size'))
                ->where($where)
                ->order('time','desc')
                ->select();
            if(!empty($data2)){
                foreach ($data2 as $k=>$v){
                  //  $data2[$k]['time'] = parent::time_ago($v['time']);
                    $data2[$k]['is_yq'] = 2;
                }
            }
            $data = array_merge($data1,$data2);

            array_multisort(array_column($data,'time'),SORT_DESC,$data);

            $newarr = array_slice($data, ($page-1)*config('page_size'), config('page_size'));

            if(!empty($newarr)){
                foreach($newarr as $k=>$v){
                    $newarr[$k]['image'] = explode(',',$v['image'])[0];
                    $newarr[$k]['time']= parent::time_ago($v['time']);
                }
            }
            $this->apiReturn('200','成功',$newarr);
        }
    }

    /*
     * 互赞消息 点赞我的
     */
    public function like()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $page = $this->p();
            Db::name("log_like")->where("send_user",$post['uid'])->setField("is_read",1);
            $where['t2.uid']  = $post['uid'];
            $data1 = Db::name('log_like')->alias('t1')
                ->field("t4.nickname,t2.title,t2.image,t2.mid as mid,t4.user_icon,t1.create_time")
                ->join('method t2','t1.object_id = t2.mid','left')
                ->join('user t4','t4.uid=t1.uid','left')
              //  ->limit(($page-1)*config('page_size'),config('page_size'))
                ->where($where)
                ->where('type',1)
                ->order("t1.create_time desc")
                ->select();
            if(!empty($data1)){
                foreach($data1 as $k=>$v){

                    $data1[$k]['video_covers'] = '';
                    $data1[$k]['status'] = 0;
                    $data1[$k]['is_yq'] = 1;
                }
            }

            $data2 = Db::name('log_like')->alias('t1')
                ->field('t4.nickname,
                t2.fc_title as title,t2.image,t2.f_id as mid,t4.user_icon,t2.video_covers,t2.status,t1.create_time')
                ->join('friends_circle t2','t1.object_id = t2.f_id','left')
                ->join('user t4','t4.uid=t1.uid','left')
               // ->limit(($page-1)*config('page_size'),config('page_size'))
                ->where($where)
                ->where('type',2)
                ->order("t1.create_time desc")
                ->select();
            if(!empty($data2)){
                foreach ($data2 as $k=>$v){

                    $data2[$k]['is_yq'] = 2;
                }
            }
            $data = array_merge($data1,$data2);
            array_multisort(array_column($data,'create_time'),SORT_DESC,$data);
            $newarr = array_slice($data, ($page-1)*config('page_size'), config('page_size'));

            if(!empty($newarr)){
                foreach($newarr as $k=>$v){
                    $newarr[$k]['image'] = explode(',',$v['image'])[0];
                    $newarr[$k]['create_time']= parent::time_ago($v['create_time']);
                }
            }
//            if(!empty($data)){
//                foreach($data as $k=>$v){
//                    $data[$k]['create_time'] = parent::time_ago($v['create_time']);
//                    $data[$k]['image'] = explode(',',$v['image'])[0];
//                }
//            }
            $this->apiReturn('200','成功',$newarr);
        }
    }

    /*
    * 修改封面
    */
    public function update_covers()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $data = [
                'food_store_cover'=>$post['food_store_cover']
            ];
            $res = Db::name('food_store')->where('food_store_id',$post['food_store_id'])->update($data);
            if($res){
                $this->apiReturn('200','修改成功');
            }else{
                $this->apiReturn('400','修改失败');
            }
        }
    }
         /**
       * [enter_status 获取我的入驻状态]
       * @Author   kendy
       * @DateTime 2020-01-10T12:22:32+0800
       * @return   [type]                   [description]
       */
    public function enter_status(){ 
         if(Request::instance()->isPost()){
                $post = Request::instance()->post();
                $uid = $post['uid'];
                 if(empty($post['uid'])){
                     $this->apiReturn('400','为传入用户id');
                 }
                $result=[];
                $result['food_enter_status'] = Db::name("food_store") 
                ->where(['uid'=>$uid,'is_del'=>0]) 
                ->value("is_pass");
                 $result['hotel_enter_status'] = Db::name("hotel")
                ->field("is_pass")
                ->where(['user_id'=>$uid,'is_del'=>0]) 
                ->value("is_pass");
                 $result['team_enter_status'] = Db::name("team")
                ->field("is_pass")
                ->where(['uid'=>$uid,'is_del'=>0]) 
                ->value("is_pass");
                if($result['team_enter_status']===null)$result['team_enter_status']=3;
                if($result['hotel_enter_status']===null)$result['hotel_enter_status']=3;
                if($result['food_enter_status']===null)$result['food_enter_status']=3;
                $this->apiReturn('200','返回成功',$result);
                
            }
    }

    //常見問題接口
    public function commonProblem()
    {
         if (Request::instance()->isPost()){
            //查询用户协议列表
            $data = Db::name('problem')->field("id,title")->select();
            foreach ($data as $key => $value) {
                $data[$key]['url']='http://www.lancly.com/api/details/problem?id=' . $value['id'];
            }
            return json(array('code'=>"200",'msg'=>'请求成功','data'=>$data));
        }
    }

     /**
     * 意见反馈
     */
    public function feedBack(){
        if (Request::instance()->isPost()){
            $post = Request::instance()->post();
            $insertData = array(
                'idea'=>$post['content'],
                'phone'=>$post['phone'],
                'create_time'=>date("Y-m-d H:i:s")
            );
            $res = Db::name('idea')->insert($insertData);
            if($res){
                return json(array('code'=>"200",'msg'=>'反馈成功'));
            }else{
                return json(array('code'=>"401",'msg'=>'反馈失败'));
            }
        }
    }
}